package com.errol.batchprocessor.employeeaddition.advisorimpl.payroll;

import com.baomidou.mybatisplus.mapper.Condition;
import com.renjia.dao.*;
import com.renjia.entity.BusPayrollRecord;
import com.renjia.entity.BusPayrollRecordDetail;
import com.renjia.entity.CoreEmployeeImportBatch;
import com.renjia.pojo.AutoCompleteFlagParser;
import com.renjia.properties.UkerServerProperties;
import com.renjia.service.CoreEmployeeImportBatchService;
import com.renjia.service.generic.log.LogService;
import com.renjia.service.generic.middleware.batchprocessor.payload.AbstractBatchPayload;
import com.renjia.service.generic.support.customer.CustomerChannelSupportService;
import com.renjia.service.generic.support.employee.BankCardSupportService;
import com.renjia.service.generic.support.employee.EmployeeCustomerSupportService;
import com.renjia.service.generic.support.employee.EmployeeSupportService;
import com.renjia.service.generic.support.payroll.PayrollDetailSupportService;
import com.renjia.service.generic.support.payroll.PayrollRecordSupportService;
import com.renjia.service.integration.threeelements.ThreeElementsService;
import com.renjia.service.module.employee.AgreementSigningService;
import com.renjia.service.module.employeeaddition.advisorimpl.EmployeeAdvisorTypeEnum;
import com.renjia.service.module.employeeaddition.advisorimpl.common.EmployeeIdentityInfoModifier;
import com.renjia.service.module.employeeaddition.advisorimpl.common.ThreeElementForceHandler;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeBatchPayload;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeePiecePayload;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeSinglePayload;
import com.renjia.service.module.messagecenter.MsgCenterSendService;
import com.renjia.service.module.payroll.advisor.verify.*;
import com.renjia.util.EpayCallingService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

/**
 * 验收编辑单条数据的扩展，只有 Single 系列扩展点会触发
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/18
 * @since v5.0.6
 **/
@Service
public class PayrollEmployeeManageAdvisorImpl extends PayrollEmployeeAdvisorImpl
        implements
        ThreeElementForceHandler<BusPayrollRecord, BusPayrollRecordDetail, PayrollEmployeeBatchPayload, PayrollEmployeePiecePayload, PayrollEmployeeSinglePayload, PayrollEmployeeManageAdvisorImpl>,
        EmployeeIdentityInfoModifier<BusPayrollRecord, BusPayrollRecordDetail, PayrollEmployeeBatchPayload, PayrollEmployeePiecePayload, PayrollEmployeeSinglePayload, PayrollEmployeeManageAdvisorImpl> {

    public PayrollEmployeeManageAdvisorImpl(CoreEmployeeImportBatchService coreEmployeeImportBatchService,
                                            CoreEmployeeMapper coreEmployeeMapper, CoreEmployeeCustomerMapper coreEmployeeCustomerMapper,
                                            CoreUserBankMapper coreUserBankMapper, CoreAgreementDetailMapper coreAgreementDetailMapper, ThreeElementsService threeElementsService,
                                            EmployeeSupportService employeeSupportService, EmployeeCustomerSupportService employeeCustomerSupportService,
                                            BankCardSupportService bankCardSupportService, CustomerChannelSupportService customerChannelSupportService,
                                            AgreementSigningService agreementSigningService, MsgCenterSendService msgCenterSendService, EpayCallingService epayCallingService, LogService logService,
                                            PayrollRecordSupportService payrollRecordSupportService,
                                            PayrollDetailSupportService payrollDetailSupportService,
                                            RedisTemplate<String, Object> redisTemplate,
                                            BusPayrollRecordMapper busPayrollRecordMapper,
                                            BusPayrollRecordDetailMapper busPayrollRecordDetailMapper,
                                            PayrollVerifyBatchStartAdvisor payrollVerifyBatchStartAdvisor,
                                            PayrollVerifySingleValidateAdvisor payrollVerifySingleValidateAdvisor,
                                            PayrollVerifySingleValidateEmployeeAdvisor payrollVerifySingleValidateEmployeeAdvisor,
                                            PayrollVerifySingleProcessAdvisor payrollVerifySingleProcessAdvisor,
                                            PayrollVerifySingleSuccessAdvisor payrollVerifySingleSuccessAdvisor,
                                            PayrollVerifySingleFailedAdvisor payrollVerifySingleFailedAdvisor,
                                            PayrollVerifySinglePostAdvisor payrollVerifySinglePostAdvisor,
                                            PayrollVerifyPiecePostAdvisor payrollVerifyPiecePostAdvisor,
                                            UkerServerProperties ukerServerProperties) {
        super(coreEmployeeImportBatchService, coreEmployeeMapper, coreEmployeeCustomerMapper, coreUserBankMapper, coreAgreementDetailMapper,
                threeElementsService, employeeSupportService, employeeCustomerSupportService, bankCardSupportService, customerChannelSupportService,
                agreementSigningService, epayCallingService, logService, payrollRecordSupportService, payrollDetailSupportService, redisTemplate,
                busPayrollRecordMapper, busPayrollRecordDetailMapper, payrollVerifyBatchStartAdvisor,
                payrollVerifySingleValidateAdvisor, payrollVerifySingleValidateEmployeeAdvisor, payrollVerifySingleProcessAdvisor,
                payrollVerifySingleSuccessAdvisor, payrollVerifySingleFailedAdvisor, payrollVerifySinglePostAdvisor,
                payrollVerifyPiecePostAdvisor, msgCenterSendService, ukerServerProperties);
    }

    @Override
    public boolean shallProcess(@NonNull AbstractBatchPayload<CoreEmployeeImportBatch, EmployeeAdvisorTypeEnum, BusPayrollRecord, BusPayrollRecordDetail> advisorPayload) {
        return EmployeeAdvisorTypeEnum.PAYROLL_RECORD_MANAGE == advisorPayload.getAdvisorTypeEnum();
    }

    @Override
    public boolean doPrepareOnSingleStart(@NonNull PayrollEmployeeSinglePayload singlePayload, @NonNull PayrollEmployeePiecePayload piecePayload, @NonNull PayrollEmployeeBatchPayload batchPayload) {
        prepareForDuplicationCheckOfDocument(batchPayload);
        piecePayload.prepareValidator();
        return payrollVerifyBatchStartAdvisor.doPrepareOnBatchStartAtVerifyStage(batchPayload);
    }

    @Override
    public boolean isBankCardModified(PayrollEmployeeSinglePayload singlePayload) {
        // 编辑发放明细，检查银行卡是否编辑
        return busPayrollRecordDetailMapper.selectCount(Condition.<BusPayrollRecordDetail>wrapper()
                .eq("id", singlePayload.getSingleEntity().getId()).eq("remark0", singlePayload.getBankCard())) == 0;
    }

    @Override
    protected boolean dealWithCoreEmployee(PayrollEmployeeSinglePayload singlePayload, PayrollEmployeePiecePayload piecePayload, PayrollEmployeeBatchPayload batchPayload) {
        if (super.dealWithCoreEmployee(singlePayload, piecePayload, batchPayload)) {
            return true;
        } else if (Boolean.FALSE.equals(singlePayload.getIsThreeElementSuccess())) {
            // 新增用户时，三要素失败，且满足一定次数，强制认证通过，并添加标识
            if (forceReviewCoreEmployee(singlePayload, piecePayload, batchPayload, this)) {
                return super.dealWithCoreEmployee(singlePayload, piecePayload, batchPayload);
            } else {
                // 强制验证通过失败
                return false;
            }
        } else {
            // 其他验证失败
            return false;
        }
    }

    @Override
    protected boolean dealWithCoreUserBank(PayrollEmployeeSinglePayload singlePayload, PayrollEmployeePiecePayload piecePayload, PayrollEmployeeBatchPayload batchPayload) {
        super.dealWithCoreUserBank(singlePayload, piecePayload, batchPayload);
        return forceReviewUserBank(singlePayload, piecePayload, batchPayload, this);
    }

    @Override
    public boolean doSettleOnSingleProcessed(@NonNull PayrollEmployeeSinglePayload singlePayload, @NonNull PayrollEmployeePiecePayload piecePayload, @NonNull PayrollEmployeeBatchPayload batchPayload) {
        if (singlePayload.getErrMsgList().isEmpty()) {
            updateValidateSuccess(singlePayload, piecePayload, batchPayload);
            // 编辑时检查自动补全的信息是否被编辑
            if (singlePayload.getSingleEntity().getAutoCompleteFlag() != null && singlePayload.getSingleEntity().getAutoCompleteFlag() > 0) {
                BusPayrollRecordDetail oldRecordDetail = this.getOldRecordDetail(singlePayload);
                singlePayload.getSingleEntity().setAutoCompleteFlag(this.checkAutoComplete(singlePayload, new AutoCompleteFlagParser(singlePayload.getSingleEntity().getAutoCompleteFlag()),
                        oldRecordDetail.getRemark0(), oldRecordDetail.getMobile()).getAutoCompleteFlag());
            }
            busPayrollRecordDetailMapper.updateById(singlePayload.getSingleEntity());
        } else {
            updateValidateFailed(singlePayload, piecePayload, batchPayload, String.join("；", singlePayload.getErrMsgList()));
        }
        return true;
    }

    @Override
    public boolean doCleanUpOnBatchPost(@NonNull PayrollEmployeeBatchPayload batchPayload) {
        return true;
    }

    private BusPayrollRecordDetail getOldRecordDetail(PayrollEmployeeSinglePayload singlePayload) {
        BusPayrollRecordDetail oldSingleEntity = singlePayload.getOldSingleEntity();
        if (oldSingleEntity == null) {
            oldSingleEntity = busPayrollRecordDetailMapper.selectById(singlePayload.getSingleEntity().getId());
            singlePayload.setOldSingleEntity(oldSingleEntity);
        }
        return oldSingleEntity;
    }
}
